/*
InstrumentDeliverySyncUtilTest
*/
global class NotiAssiantUtil {

    //发货申请通知助理
    WebService static String noteDelivery(String id) {
        SavePoint sp  = Database.setSavepoint();
        try {
           
            
            InstrumentDeliveryApplication__c ida = [select InventoryStatus__c,NotifiedAssistantTime__c, Id,ReceivingCustomer__c,ReceivingCustomer__r.Name ,OwnerId  from InstrumentDeliveryApplication__c where Id =: id ];
            
            if(ida.InventoryStatus__c == null ) return '请填写出库公司!';
            

           // if(company == null )return '请联系管理员维护合同Corpcode__c公司信息';
            List<UserInformation__c> salers = [select OrderProcessingAssistant__c  from UserInformation__c where Subcompany__c=: ida.InventoryStatus__c limit 1 ];

            if(salers.isEmpty()) return '没有找到用户对照表:'+ida.InventoryStatus__c;
            UserInformation__c saler = salers[0];
            

            if(ida.NotifiedAssistantTime__c  != null ) return '已经通知助理,请不要重复点击!';
            if(saler.OrderProcessingAssistant__c == null )return '通知失败:请联系系统管理员维护订单助理信息!';
            if(ida.ReceivingCustomer__c == null )return '请明确收货客户!';

            //共享单据
            //InstrumentDeliveryApplication__Share
            InstrumentDeliveryApplication__Share  idashare = new InstrumentDeliveryApplication__Share();
            idashare.AccessLevel = 'Read';
            idashare.ParentId = ida.Id ;
            idashare.UserOrGroupId = saler.OrderProcessingAssistant__c ;
            if(saler.OrderProcessingAssistant__c != ida.OwnerId) insert idashare;
            
            Task t = new Task();
            t.OwnerId = saler.OrderProcessingAssistant__c;
            t.Subject = '请核实收货客户:'+ida.ReceivingCustomer__r.Name+ '出库产品并同步NC!'  ;
            t.Description = '请核实收货客户:'+ida.ReceivingCustomer__r.Name+ '出库产品并同步NC!'  ;
            t.Priority = 'High';
            t.WhatId = ida.Id;
            t.ActivityDate = Date.valueOf(System.now()); 
            System.debug('报价通知'+t);
            insert t ;

            ida.NotifiedAssistantTime__c = System.today();
            update ida ; 
            return '已经通知助理!';
            
        }
        catch (Exception e) {
            Database.rollback(sp);
            return '通知失败:'+e.getMessage();
        }


    }
    //合同通知助理 
    WebService static String noteContract(String id) {
        SavePoint sp  = Database.setSavepoint();
        try {
            Contract__c con = [select Id ,Account__r.Name ,Corpcode__c,NotifyAssistantTime__c from Contract__c where Id=:id ];
            Integer cnt = [select count() from ContractProduct__c where Contract__c =:id ];
            cnt += [select count() from ContractReagentInformation__c where Contract__c =:id ];
            if(cnt == 0) return '没有合同产品不能通知助理!';
            if(con.NotifyAssistantTime__c != null) return '已通知助理,请勿重复通知!';
            String company = ImportObject.getPicklistValues('Contract__c', 'Corpcode__c', true).get(con.Corpcode__c);

            if(company == null )return '请联系管理员维护合同Corpcode__c公司信息';
            List<UserInformation__c> userinos  = [select SalesAssistant__c  from UserInformation__c where Subcompany__c=: company limit 1 ];
            UserInformation__c assiant   = userinos[0];
            if(assiant.SalesAssistant__c == null )return '通知失败:请联系系统管理员维护销售助理信息!';

            
            Task t = new Task();
            t.OwnerId = assiant.SalesAssistant__c ;
            t.Subject = con.Account__r.Name +'有合同需要您确认后，点击“同步OA”。同步成功后，请在OA代办流程中将此合同提交审批！'  ;
            t.Description = con.Account__r.Name +'有合同需要您确认后，点击“同步OA”。同步成功后，请在OA代办流程中将此合同提交审批！'  ;
            t.Priority = 'High';
            t.WhatId = con.Id;
            t.ActivityDate = Date.valueOf(System.now()); 
            insert t ;

            con.NotifyAssistantTime__c = System.now();
            update con;
            return '已经通知助理!';

        }
        catch (Exception e) {
            Database.rollback(sp);
            return '通知失败:'+e.getMessage(); 
        }
        

        
    }
        //合同通知助理 
    WebService static String noteOpp(String id) {
        SavePoint sp  = Database.setSavepoint();
        try {
            Opportunity con = [select Id, NoticeTime__c from Opportunity where Id=:id ];
            
            if(con.NoticeTime__c != null )throw new DmlException('已经通知过助理!不要重复通知!');
            con.NoticeTime__c = System.today();
            update con;
            return '已经通知助理!';

        }
        catch (Exception e) {
            Database.rollback(sp);
            return '通知失败:'+e.getMessage(); 
        }
        

        
    }
    
    
   /* //报价通知助理
    WebService static String noteQoute(String qid) {
    	SavePoint sp  = Database.setSavepoint();
    	try {
    		Quote q = [select Id ,isNotedAssiant__c,Opportunity.Account.NCICCode__c, Opportunity.RecordType.DeveloperName , Opportunity.ExecutiveCompany__r.NCICCode__c ,Opportunity.ExecutiveCompany__r.RecordType.DeveloperName ,Status ,Opportunity.Owner.SalesAssistant__c from Quote where Id=:qid];
            System.debug('+++'+q);

    		if(q.isNotedAssiant__c == true ) return '已经通知过助理!';
            String nciccode = (q.Opportunity.Account.RecordType.DeveloperName =='Maccura' || q.Opportunity.RecordType.DeveloperName != 'CustomerOpportunity' )? q.Opportunity.Account.NCICCode__c : q.Opportunity.ExecutiveCompany__r.NCICCode__c ;
            if(nciccode == null || nciccode.length() ==0 ) return'客户没有准入,请先提起准入申请!';
    		if(q.Status != '已批准')return '报价未批准不能操作!';
            System.debug('+++'+q.Opportunity.Owner.SalesAssistant__c);
    		if(q.Opportunity.Owner.SalesAssistant__c == null ) return '没有维护销售助理!请联系系统管理员';
    		Task t = new Task();
    		t.OwnerId = q.Opportunity.Owner.SalesAssistant__c;
    		t.Subject = '审批通过的报价,请及时同步NC系统!';
	        t.Description = '审批通过的报价,请及时同步NC系统!'; 
	        t.Priority = 'High';
            t.WhatId = q.Id;
	        t.ActivityDate = Date.valueOf(System.now()); 
            System.debug('报价通知'+t);
	        insert t ;

	        q.isNotedAssiant__c = true;
	        update q;
	        return '已经通知助理!';
    	    
    	}
    	catch (Exception e) {
    	    return '通知失败:'+e.getMessage();
    	}
    	
    	
    }
    */
/*
    WebService static String noteMeasure(String mid){
    	SavePoint sp  = Database.setSavepoint();
    	try {
    		Measure_Calculate__c q = [select Id ,isNotedAssiant__c ,Status__c ,Opportunity__r.Owner.SalesAssistant__c from Measure_Calculate__c where Id=:mid];

    		if(q.isNotedAssiant__c == true ) return '已经通知过助理!';
    		if(q.Status__c != '审批成功')return '测算未批准不能操作!';
    		if(q.Opportunity__r.Owner.SalesAssistant__c == null ) return '没有维护销售助理!请联系系统管理员';
    		Task t = new Task();
    		t.OwnerId = q.Opportunity__r.Owner.SalesAssistant__c;
    		t.Subject = '审批通过的测算,请及时同步NC系统!';
	        t.Description = '审批通过的测算,请及时同步NC系统!'; 
	        t.Priority = 'High';
            t.WhatId = q.Id;
	        t.ActivityDate = Date.valueOf(System.now()).addDays(1); 
            System.debug('测算通知'+t);
	        insert t ;

	        q.isNotedAssiant__c = true;
	        update q;

	        return '已经通知助理!';
    	    
    	}
    	catch (Exception e) {
    	    return '通知失败:'+e.getMessage();
    	}
    }*/
}